package io.zhengsh.order.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

@ControllerAdvice
@ResponseBody
public class ExceptionHandlerAdvice {

    protected Logger logger = LoggerFactory.getLogger(getClass().getSimpleName());

    @ExceptionHandler(value = BindException.class)
    public String exception(BindException exception, WebRequest request) {
        logger.warn("BindException 异常信息:", exception);
        return exception.getMessage();
    }

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public String exception(MethodArgumentNotValidException exception, WebRequest request) {
        logger.info("MethodArgumentNotValidException 异常信息:", exception);
        return exception.getBindingResult().getFieldError().getDefaultMessage();
    }
}